import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login'
import home from '@/views/home'
import self from '@/components/home/self/self'
import table from '@/components/home/self/table'
import table1 from '@/components/home/self/table1'
import table2 from '@/components/home/self/table2'
import table3 from '@/components/home/self/table3'
import table4 from '@/components/home/self/table4'
import post from '@/components/home/art/postArt'
import edit from '@/components/home/art/editArt'
import show from '@/components/home/art/showArt'
import chat from '@/components/home/chat/chat'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home
  },
  {
    path: '/post',
    component: post
  },
  {
    path: '/edit',
    name: 'edit',
    component: edit
  },
  {
    path: '/show',
    name: 'show',
    component: show
  },
  {
    path: '/self',
    component: self,
    children: [
      {
        path: '/self/table',
        component: table
      },
      {
        path: '/self/table1',
        component: table1
      },
      {
        path: '/self/table2',
        component: table2
      },
      {
        path: '/self/table3',
        component: table3
      },
      {
        path: '/self/table4',
        component: table4
      }
    ]
  },
  {
    path: '/chat',
    name: 'chat',
    component: chat
  },
  {
    path: '*',
    redirect: '/login'
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    if (window.localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
